<template>
  <div class="chat-container"  v-loading="loading">
    <el-card>
      <template #header>
        <div class="header">
          <span>聊天记录</span>
          <el-input
            v-model="searchQuery"
            placeholder="搜索聊天记录"
            style="width: 200px"
          />
        </div>
      </template>

      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="userId" label="用户ID" />
        <el-table-column prop="chatId" label="会话ID" />
        <el-table-column prop="userName" label="用户名" />
        <el-table-column prop="message" label="会话内容" show-overflow-tooltip />
        <el-table-column prop="knowledgeId" label="知识库ID" />
        <el-table-column prop="modelId" label="模型ID" />
        <el-table-column prop="tokens" label="消耗Token" />
        <el-table-column prop="promoteTokens" label="请求Token" />
        <el-table-column prop="ctime" label="创建时间" />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" @click="handleView(scope.row)">查看</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
        <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

   

    <!-- 查看聊天记录对话框 -->
    <el-dialog
      title="聊天记录详情"
      v-model="detailDialogVisible"
      width="600px"
    >
      <el-descriptions :column="1" border>
        <el-descriptions-item label="用户ID">{{ detailData.userId }}</el-descriptions-item>
        <el-descriptions-item label="会话ID">{{ detailData.chatId }}</el-descriptions-item>
        <el-descriptions-item label="用户名">{{ detailData.userName }}</el-descriptions-item>
        <el-descriptions-item label="会话内容">{{ detailData.message }}</el-descriptions-item>
        <el-descriptions-item label="知识库ID">{{ detailData.knowledgeId }}</el-descriptions-item>
        <el-descriptions-item label="模型ID">{{ detailData.modelId }}</el-descriptions-item>
        <el-descriptions-item label="消耗Token">{{ detailData.tokens }}</el-descriptions-item>
        <el-descriptions-item label="请求Token">{{ detailData.promoteTokens }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ detailData.ctime }}</el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="detailDialogVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'

import { request } from '../../composables/useCommon'

// 表格数据
const tableData = ref([])

// 表格 loading 状态
const loading = ref(false)

// 获取表格数据
const getTableData = async () => {
  try {
    loading.value = true
    const res = await request('ragMessage/', 'get', {
      pageNo: currentPage.value,
      pageSize: pageSize.value,
      name: searchQuery.value
    })
    tableData.value = res.data.records
    total.value = res.data.total
  } catch (err) {
    ElMessage.error('获取聊天记录列表失败')
  } finally {
    loading.value = false
  }
}

// 新增聊天记录
const addMessage = async (messageData) => {
  try {
    await request('ragMessage/', 'post', messageData)
    ElMessage.success('聊天记录添加成功')
    getTableData()
  } catch (err) {
    ElMessage.error('添加聊天记录失败')
  }
}

// 修改聊天记录
const updateMessage = async (messageData) => {
  try {
    await request('ragMessage/', 'put', messageData)
    ElMessage.success('聊天记录更新成功')
    getTableData()
  } catch (err) {
    ElMessage.error('更新聊天记录失败')
  }
}

// 删除聊天记录
const deleteMessage = async (messageId) => {
  try {
    await request(`ragMessage/${messageId}`, 'delete')
    ElMessage.success('聊天记录删除成功')
    getTableData()
  } catch (err) {
    ElMessage.error('删除聊天记录失败')
  }
}

// 搜索
const searchQuery = ref('')
const handleSearch = () => {
  currentPage.value = 1
  getTableData()
}

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}
const handleCurrentChange = (val) => {
  currentPage.value = val
  getTableData()
}

// 查看详情对话框
const detailDialogVisible = ref(false)
const detailData = reactive({
  userId: '',
  chatId: '',
  userName: '',
  message: '',
  knowledgeId: '',
  modelId: '',
  tokens: '',
  promoteTokens: '',
  ctime: ''
})

// 处理查看详情
const handleView = (row) => {
  Object.assign(detailData, row)
  detailDialogVisible.value = true
}

// 处理删除聊天记录
const handleDelete = (row) => {
  ElMessageBox.confirm(
    '确认要删除该聊天记录吗？',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(async () => {
    try {
      await deleteMessage(row.id)
    } catch (err) {
      ElMessage.error('删除失败')
    }
  })
}

// 获取表格数据
getTableData()
</script>

<style scoped>
.chat-container {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

</style>
